<?php

namespace App\Http\Controllers;

use App\UserModel;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class AdminController extends Controller
{
    function form(Request $request) {
        $id = $request->input("id",0);
        if($id==0){
            $uqur = null;
        }else{
            $uqur = UserModel::find($id);
        }
        return view('form',compact('uqur'));
    }

    public function add(Request $request)
    {
        $inputs = $request->input();
        unset($inputs['_token']);
        if($inputs['id']==0){
            $inputs['created_at'] = date('Y-m-d H:i:s');
            $ok = UserModel::create($inputs);
            $msg = "已填写数据"."<a href='table'>返回首页</a>";
        }else{
//            $uqur = UserModel::find($inputs['id']);
//            $uqur->name = $inputs['name'];
//            $uqur->pwd = $inputs['pwd'];
//            $uqur->save();
//            $msg = "数据更改成功"."<a href='table'>返回首页</a>";

            UserModel::where('id',$inputs['id'])->update($inputs);
            $msg = "数据更改成功"."<a href='table'>返回首页</a>";
        }
        return $msg;
    }

    public function table(Request $request)
    {
        $uqur = UserModel::orderby('created_at','asc')->get();
        return view("table",compact('uqur'));
    }

    public function del(Request $request)
    {
        $id = $request->input('id');
        UserModel::find($id)->delete();
        return "删除成功"."<a href='table'>返回首页</a>";
    }
}
